/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mantenimiento.dao;

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;

public class MailDAO {
private    String host = "smtpseguro.terra.com.pe";
private  String  transporte= "smtp";
private  String puerto= "25";
 private        String  user = "4fantasticos_transporte@terra.com.pe";
 private        String pass="melchor";
 private      String from = "4fantasticos_transporte@terra.com.pe";
 private      String to = "";
 private      String cc = null;
 private      String bcc = null;
 private  String  subject="";
 private  String mess="";
 private      String directorio = null;
  private      String archivo = null;


   public  boolean enviarCorreo(String para,int codigo ,String clave){
     boolean  b = false;
      to =para;
     this. mess ="Bienvenido al Sistema de Mantenimiento" +
             "Su codigo de ingreso al sistema es: "+codigo+"."+"Su clave es: " + clave+"." ;
     this.subject= "Registro";

    b= enviar();
    return b;
 }

   public  boolean enviarCorreo(String para, String cc,  String subject , String mens){
     boolean  b = false;
      to =para;
      this.cc=cc;
     this. mess = mens;
     this.subject= subject;

    b= enviar();
    return b;
 }

   public  boolean enviarCorreo(String para, String cc, String bcc, String subject , String mens){
     boolean  b = false;
      to =para;
      this.cc=cc;
       this.bcc=bcc;
     this. mess = mens;
     this.subject= subject;

    b= enviar();
    return b;
 }
 public  boolean enviarCorreo(String para, String cc, String bcc,
         String subject , String mens, String directorio, String archivo){
     boolean  b = false;
      to =para;
      this.cc=cc;
       this.bcc=bcc;
     this. mess = mens;
     this.subject= subject;
     this.directorio=directorio;
     this.archivo=archivo;
    b= enviar();
    return b;
 }

 private   boolean  enviar(){
     boolean  b = false;
      try {
     Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", user);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", puerto); // 587 is the port number of yahoo mail
        props.put("mail.smtp.auth", "true");
        Session session = Session.getDefaultInstance(props, null);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipients(Message.RecipientType.TO, to);
        if ( cc!=null){
            message.addRecipients(Message.RecipientType.CC, cc);
        }
        if ( bcc!=null){
            message.addRecipients(Message.RecipientType.BCC, bcc);
        }

          message.setSubject(subject);
        message.setText(mess);
         /////////////
        // Create the message part
        BodyPart messageBodyPart = new MimeBodyPart();
         messageBodyPart.setText(mess);
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
       if (archivo!=null){
        // Part two is attachment
        messageBodyPart = new MimeBodyPart();
        String filename = directorio+archivo;
        System.out.println(filename);
        DataSource source = new FileDataSource(filename);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(archivo);
        multipart.addBodyPart(messageBodyPart);

        // Put parts in message
        message.setContent(multipart);

       }
        /////////////




        Transport transport = session.getTransport(transporte);
        transport.connect(host, user, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        b= true;
        System.out.println(" enviado...");
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Error al enviar correo: " + e);
    }
     return b;
}




}


